using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scripting;

public class InventoryManager : MonoBehaviour
{
    public static InventoryManager instance;
    public int maxStackedItems = 4; //最大堆叠数量，默认4
    public InventorySlot[] inventorySlots; // 背包槽数组
    public GameObject inventoryItemPrefab; // 物品预制体
    public Item[] startItems; //默认库存信息

    int selectedSlot = -1;

    void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        //判断inventorySlots是否为空
        if (inventorySlots.Length <= 0)
        {
            Debug.Log("背包槽数组没有配置，请先配置好！");
            return;
        }
        ChangeSelectedSlot(0);//默认选中第一个槽
        foreach (var item in startItems)
        {
            AddItem(item);
        }
    }

    // private void Update(){
    //     if (Input.GetKeyDown (KeyCode.Alpha1))
    //         ChangeSelectedSlot(0);
    //     else if (Input.GetKeyDown(KeyCode.Alpha2))
    //         ChangeSelectedSlot(1);
    //     else if (Input.GetKeyDown(KeyCode.Alpha3))
    //         ChangeSelectedSlot(2);
    //     else if (Input.GetKeyDown(KeyCode.Alpha4))
    //         ChangeSelectedSlot(3);
    //     else if (Input.GetKeyDown(KeyCode.Alpha5))
    //         ChangeSelectedSlot(4);
    //     else if (Input.GetKeyDown(KeyCode.Alpha6))
    //         ChangeSelectedSlot(5);
    //     else if (Input.GetKeyDown(KeyCode.Alpha7))
    //         ChangeSelectedSlot(6);
    // }
    private void Update()
    {
        if (Input.inputString != null)
        {
            bool isNumber = int.TryParse(Input.inputString, out int number);
            if (isNumber & number > 0 & number < 8) ChangeSelectedSlot(number - 1);
        }
    }

    void ChangeSelectedSlot(int newValue)
    {
        if (selectedSlot >= 0)
        {
            inventorySlots[selectedSlot].Deselect();// 取消之前选中的槽位
        }
        inventorySlots[newValue].Select();// 选择新的槽位
        selectedSlot = newValue;// 更新选中的槽位索引
    }

    // 添加物品到背包
    public bool AddItem(Item item)
    {
        for (int i = 0; i < inventorySlots.Length; i++)
        {
            InventorySlot slot = inventorySlots[i]; // 获取当前遍历的背包槽
            InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>(); // 在背包槽中查找是否已经存在物品

            if (itemInSlot == null) // 如果背包槽内没有物品
            {
                SpawnNewItem(item, slot); // 生成新的物品到这个背包槽中
                return true;
            }
            else if (itemInSlot.item == item && itemInSlot.count < maxStackedItems && itemInSlot.item.stackable == true)
            {
                itemInSlot.count++;//添加数量
                itemInSlot.RefreshCount();
                return true;
            }
        }
        return false;
    }

    // 生成新的物品到背包槽中
    void SpawnNewItem(Item item, InventorySlot slot)
    {
        GameObject newItemGo = Instantiate(inventoryItemPrefab, slot.transform); // 实例化物品预制体并设置父级为当前的背包槽
        InventoryItem inventoryItem = newItemGo.GetComponent<InventoryItem>(); // 获取生成物品的 InventoryItem 组件
        inventoryItem.InitialiseItem(item); // 初始化物品信息
    }

    // 获取当前选中物品
    public Item GetSelectedItem(bool use)
    {
        if (inventorySlots.Length > 0)
        {
            InventorySlot slot = inventorySlots[selectedSlot];// 获取当前选中槽位
            InventoryItem itemInSlot = slot.GetComponentInChildren<InventoryItem>();// 获取槽位上的物品
            if (itemInSlot != null)
            {
                Item item = itemInSlot.item;
                //是否使用物品
                if (use == true)
                {
                    itemInSlot.count--;//减少库存
                    if (itemInSlot.count <= 0)
                    {
                        Destroy(itemInSlot.gameObject);//删除物品
                    }
                    else
                    {
                        itemInSlot.RefreshCount();//更新数量文本显示
                    }
                }
                return item;
            }
        }
        return null;//如果没有选中物品则返回null
    }

}
